<?php

namespace App\Admin\Actions\Grid;

use App\Models\Alarm;
use Dcat\Admin\Actions\Response;
use Dcat\Admin\Grid\Tools\AbstractTool;
use Dcat\Admin\Traits\HasPermissions;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class AlarmReset extends AbstractTool
{
    protected $style = 'btn btn-primary btn-outline';

    /**
     * @return string
     */
    protected $title = '全部恢复';

    /**
     * Handle the action request.
     *
     * @param Request $request
     *
     * @return Response
     */
    public function handle(Request $request)
    {
        $alarmList = Alarm::where(['status' => '报警'])->get();

        foreach ($alarmList as $aItem) {
            /**
             * @var Alarm $aItem
             */
            $aItem->selfRecovery();
        }

        return $this->response()
            ->success('执行成功')
            ->refresh();
    }

    /**
     * @return string|void
     */
    protected function href()
    {
        // return admin_url('auth/users');
    }

    /**
     * @return string|array|void
     */
    public function confirm()
    {
         return ['提醒', '确认全部恢复？'];
    }

    /**
     * @param Model|Authenticatable|HasPermissions|null $user
     *
     * @return bool
     */
    protected function authorize($user): bool
    {
        return true;
    }

    /**
     * @return array
     */
    protected function parameters()
    {
        return [];
    }
}
